package com.inspinia.base.dao.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.inspinia.base.dao.config.DataSourceConfig;
import com.inspinia.config.common.config.DbConfig;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 *
 * @author veblen
 * @date 2017/1/13
 */
@Component
public class MasterDataSource implements CustomDataSource {

    DruidDataSource dataSource;

    @Override
    public DataSource getDataSource() {
        if (dataSource == null) {
            dataSource = new DruidDataSource();
            dataSource.setDriverClassName(DbConfig.driver);
            dataSource.setUrl(DbConfig.url);
            dataSource.setUsername(DbConfig.username);
            dataSource.setPassword(DbConfig.password);

            dataSource.setTestWhileIdle(true);
            dataSource.setTestOnBorrow(false);
            dataSource.setTestOnReturn(false);
            dataSource.setInitialSize(10);
            dataSource.setPoolPreparedStatements(true);
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
            dataSource.setRemoveAbandonedTimeout(DbConfig.druidRemoveabAndOnedTimeout);
            dataSource.setMinIdle(1);
            dataSource.setMaxActive(DbConfig.druidMaxActive);
            dataSource.getProxyFilters().add(DataSourceConfig.slf4jLogFilter());
        }
        return dataSource;
    }

    @Override
    public DataSources getKey() {
        return DataSources.MASTER;
    }
}
